function [pp, hr, la, uo, co] = get_meantimeseries(ptstruct,ids,win,noverlap,ismean)
%GET_MEANTIMESERIES	get averaged time series according to the specified time window
%	[pp, hr] = get_meantimeseries(ptstruct,ids,win,noverlap,ismean) 
%	
%
%	Inputs:     ptstruct    patient structure that contains ids
%               ids         vector with ids of the patients of interest
%               win         size of window (in hours)
%               noverlap    value of overlap (e.g., 0 - no overlap)
%               ismean      1 determine mean of values of each patient
%		
%		
%	Outputs:    pp, hr      pulsep and hr averaged time series (Nx4):
%                           columns 1 - time, 2 - average value, 3 - SE,
%                           4 - median, 5 - 75th prctile, 6 - 25th prctile,
%                           7 - number of patients included, 8 - 4/N
%		
%
%
%	
%	See also ana_dynamics

%	References: N/A
%	
%	

%	Copyright 2013 MAF Pimentel
%	This program is free software: you can redistribute it and/or modify
%	it under the terms of the GNU General Public License as published by
%	the Free Software Foundation, either version 3 of the License, or
%	(at your option) any later version.
%	
%	This program is distributed in the hope that it will be useful,
%	but WITHOUT ANY WARRANTY; without even the implied warranty of
%	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%	GNU General Public License for more details.
%	
%	You should have received a copy of the GNU General Public License
%	along with this program.  If not, see <http://www.gnu.org/licenses/>.


%	$Author: MAF Pimentel$
%	$Revision: 1.0.0.0$
%	$Date: 31-May-2013 11:22:02$
%	Contact: marco.and.pimentel@gmail.com
%	Originally written on: PCWIN64

if nargin < 5
    ismean = 1;
end

if nargin < 4
    noverlap = 0;
end

if nargin < 3
    win = 24;
end

% define limits for hr and pp data
LIMITShr = [20 400];
LIMITSpp = [0 300];

los = [];
for i = ids(:)'
    los = [los ptstruct(i).los];
end


win = win * 60;
time = win:win*(1-noverlap):prctile(los,65)*24*60;
nwin = length(time);

pp = zeros(nwin,8); hr = zeros(nwin,8);
la = zeros(nwin,8); uo = zeros(nwin,8);
co = zeros(nwin,8);

datapp{nwin} = []; datahr{nwin} = [];
datala{nwin} = []; datauo{nwin} = [];
dataco{nwin} = []; npts{nwin} = [];

for n = 1 : nwin
    datapp{n} = []; datahr{n} = []; 
    datala{n} = []; datauo{n} = []; npts{n} = [];
    
    for i = ids(:)'
        indpp = find(time(n)-win <= ptstruct(i).data{1}.pulsep(:,1) & ...
            ptstruct(i).data{1}.pulsep(:,1) < time(n));
        
        indhr = find(time(n)-win <= ptstruct(i).data{1}.hr(:,1) & ...
            ptstruct(i).data{1}.hr(:,1) < time(n));
        
        indla = find(time(n)-win <= ptstruct(i).data{1}.lactate(:,1) & ...
            ptstruct(i).data{1}.lactate(:,1) < time(n));
        
        
        induo = find(time(n)-win <= ptstruct(i).data{1}.uo(:,1) & ...
            ptstruct(i).data{1}.uo(:,1) < time(n));

        if ~isempty(indpp)
            dataprov = ptstruct(i).data{1}.pulsep(indpp,2);
            dataprov(dataprov < LIMITSpp(1) & dataprov > LIMITSpp(2)) = [];
            if ismean
                datapp{n} = [datapp{n}; mean(dataprov)];
            else
                datapp{n} = [datapp{n}; dataprov(:)];
            end
            
            pp(n,7) = pp(n,7) + 1;
        end
        
        if ~isempty(indhr)
            dataprov = ptstruct(i).data{1}.hr(indhr,2);
            dataprov(dataprov < LIMITShr(1) & dataprov > LIMITShr(2)) = [];
            if ismean
                datahr{n} = [datahr{n}; mean(dataprov)];
            else
                datahr{n} = [datahr{n}; dataprov(:)];
            end
            
            hr(n,7) = hr(n,7) + 1;
        end
        
        if ~isempty(indpp) && ~isempty(indhr)
            if ismean
                dataco{n} = [dataco{n}; datahr{n}(end)*datapp{n}(end)];
            else
                dataco{n} = [NaN];
            end
            
            co(n,7) = co(n,7) + 1;
        end
        
        if ~isempty(indla)
            dataprov = ptstruct(i).data{1}.lactate(indla,2);
            %dataprov(dataprov < LIMITSpp(1) & dataprov > LIMITSpp(2)) = [];
            if ismean
                datala{n} = [datala{n}; mean(dataprov)];
            else
                datala{n} = [datala{n}; dataprov(:)];
            end
            
            la(n,7) = la(n,7) + 1;
        end
        
        if ~isempty(induo)
            dataprov = ptstruct(i).data{1}.uo(induo,2);
            %dataprov(dataprov < LIMITSpp(1) & dataprov > LIMITSpp(2)) = [];
            if ismean
                datauo{n} = [datauo{n}; mean(dataprov)];
            else
                datauo{n} = [datauo{n}; dataprov(:)];
            end
            
            uo(n,7) = uo(n,7) + 1;
        end
        
    end
    
    pp(n,1) = time(n)/(24*60); pp(n,2) = mean(datapp{n}); 
    pp(n,3) = std(datapp{n})/sqrt(length(datapp{n}));
    pp(n,4) = median(datapp{n}); pp(n,5) = prctile(datapp{n},75);
    pp(n,6) = prctile(datapp{n},25); %pp(n,7) = length(datapp{n});
    pp(n,8) = pp(n,7)/length(ids);
    
    hr(n,1) = time(n)/(24*60); hr(n,2) = mean(datahr{n}); 
    hr(n,3) = std(datahr{n})/sqrt(length(datahr{n}));
    hr(n,4) = median(datahr{n}); hr(n,5) = prctile(datahr{n},75);
    hr(n,6) = prctile(datahr{n},25); hr(n,7) = length(datahr{n});
    hr(n,8) = hr(n,7)/length(ids);
    
    la(n,1) = time(n)/(24*60); la(n,2) = mean(datala{n}); 
    la(n,3) = std(datala{n})/sqrt(length(datala{n}));
    la(n,4) = median(datala{n}); la(n,5) = prctile(datala{n},75);
    la(n,6) = prctile(datala{n},25); la(n,7) = length(datala{n});
    la(n,8) = la(n,7)/length(ids);
    
    uo(n,1) = time(n)/(24*60); uo(n,2) = mean(datauo{n}); 
    uo(n,3) = std(datauo{n})/sqrt(length(datauo{n}));
    uo(n,4) = median(datauo{n}); uo(n,5) = prctile(datauo{n},75);
    uo(n,6) = prctile(datauo{n},25); uo(n,7) = length(datauo{n});
    uo(n,8) = uo(n,7)/length(ids);
    
    co(n,1) = time(n)/(24*60); co(n,2) = mean(dataco{n}); 
    co(n,3) = std(dataco{n})/sqrt(length(dataco{n}));
    co(n,4) = median(dataco{n}); co(n,5) = prctile(dataco{n},75);
    co(n,6) = prctile(dataco{n},25); co(n,7) = length(dataco{n});
    co(n,8) = co(n,7)/length(ids);
    
end

end